class game extends eui.Component implements eui.UIComponent {
	public constructor(father, prama) {
		super();
		this.skinName = "gameSkin";
		this.father = father;
		this.prama = prama;
		//背景定时器
		this.timer4 = new egret.Timer(8000, 0);
		this.timer4.addEventListener(egret.TimerEvent.TIMER, this.timergame, this);
		this.timer4.start();

		//随机生成云
		this.timer5 = new egret.Timer(1500, 0);
		this.timer5.addEventListener(egret.TimerEvent.TIMER, this.suiji, this);
		this.timer5.start();

		var timer7 = new egret.Timer(1, 0);
		timer7.addEventListener(egret.TimerEvent.TIMER, function () {
			this.c1.y += 15;
			this.c2.y += 10;
		}, this);
		timer7.start();

		//随机生成瓶子
		this.timer8 = new egret.Timer(5000, 0);
		this.timer8.addEventListener(egret.TimerEvent.TIMER, this.pin, this);
		this.timer8.start();

		var timer10 = new egret.Timer(1, 0);
		timer10.addEventListener(egret.TimerEvent.TIMER, function () {
			this.num1 = Math.floor(Math.random() * 440);
			this.num2 = Math.floor(Math.random() * 540);
		}, this);
		timer10.start();

		this.timer11 = new egret.Timer(1000, 0);
		this.timer11.addEventListener(egret.TimerEvent.TIMER, this.time, this);
		this.timer11.start();

		this.c = new egret.Bitmap();
		this.num = Math.floor(Math.random() * 100);
		this.c1 = new egret.Bitmap();
		this.c2 = new egret.Bitmap();

	}
	private c;
	private num;
	private c1;
	private num1;
	private c2;
	private num2;
	private bottom1;
	private bottom2;
	private right1;
	private right2;
	// 父容器
	public father;
	// 传来的参数
	public prama;
	private game1;
	private game2;
	private game3;
	private game4;
	private game5;
	private game6;
	private game7;
	private game8;
	private game9;
	private game10;
	private number;
	private number1;
	private image;
	private image1;
	private image2;
	private image3;
	private image4;
	private image5;
	private image6;
	private image7;
	private image8;
	private offsetX;
	private timer4;
	private timer5;
	private timer8;
	private timer9;
	private timer11;
	private group2;
	private group3;
	private group5;
	private label1;
	private label2;
	private label3;
	private label4;
	private label5;
	private label6;
	//开关
	private kaiguan = true;

	protected partAdded(partName: string, instance: any): void {
		super.partAdded(partName, instance);
	}


	protected childrenCreated(): void {
		super.childrenCreated();

		//背景和球的动画
		this.game2.play(-1);
		this.game1.play(-1);
		this.number.addEventListener(egret.Event.ENTER_FRAME, this.text, this);
		//监听碰撞事件
		this.addEventListener(egret.Event.ENTER_FRAME, this.pengzhuan, this);
		//监听手指触摸球移动事件
		this.image.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.startMove, this);
		this.image.addEventListener(egret.TouchEvent.TOUCH_END, this.stopMove, this);

		this.image1.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.tiao, this);
		this.image2.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.tiao1, this);
		this.image3.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.tiao2, this);
		this.image4.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.tiao2, this);
		this.image5.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.qiehuan, this);
		this.image6.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.Group5, this);
		this.image7.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.tiao1, this);
		this.image8.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.Label1, this);

		this.label1.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.Label1, this);
		this.label2.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.tiao1, this);
		this.label3.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.Label3, this);
		this.label4.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.Label1, this);
		this.label5.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.tiao1, this);
		this.label6.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.Label3, this);


	}
	timergame() {
		this.game1.play(-1);
	}
	text() {
		var n = Number(this.number.text);
		var k = Number(this.number1.text);
		n += 1;
		k += 1;
		this.number.text = n;
		this.number1.text = k;
	}
	startMove(e) {
		//把手指按到的对象记录下来
		this.image = e.currentTarget;
		//计算手指和要拖动的对象的距离
		this.offsetX = e.stageX - this.image.x;
		//手指在屏幕上移动，会触发 onMove 方法
		this.image.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.onMove, this);
	}
	stopMove() {
		this.image.removeEventListener(egret.TouchEvent.TOUCH_MOVE, this.onMove, this);
	}
	onMove(e) {
		var n = e.stageX - this.offsetX;
		if (n >= 0 && n <= this.stage.stageWidth - this.image.width) {
			this.image.x = n;
		}
	}
	//云
	suiji() {
		this.c1.texture = RES.getRes("game-yu_png");
		this.addChild(this.c1);
		this.c1.width = 200;
		this.c1.height = 200;
		this.c1.y = -200;
		this.c1.x = this.num1;
	}
	//瓶子
	pin() {

		this.c2.texture = RES.getRes("pingzi_png");
		this.addChild(this.c2);
		this.c2.width = 100;
		this.c2.height = 240;
		this.c2.y = -240;
		this.c2.x = this.num2;

	}
	tiao() {
		this.game4.play(-1);
	}
	tiao1() {
		sceneManager.fun().loading(this.father, player, "player");
	}
	tiao2() {
		this.game5.play(-1);

		// let c = new egret.Bitmap();
		// let num = Math.floor(Math.random() * 100);
		if (this.num >= 0 && this.num < 60) {
			this.c.texture = RES.getRes("result-2_png");
			this.addChild(this.c);
			this.c.width = 336;
			this.c.height = 103;
			this.c.y = 503;
			this.c.x = 152;
			this.c.touchEnabled = true;
			this.c.addEventListener(egret.TouchEvent.TOUCH_BEGIN, function () {
				this.game6.play(-1);
				this.removeChild(this.c);
			}, this);
		} else if (this.num >= 60 && this.num < 90) {
			this.c.texture = RES.getRes("result-1_png");
			this.addChild(this.c);
			this.c.width = 387;
			this.c.height = 79;
			this.c.y = 497;
			this.c.x = 127;
		} else if (this.num >= 90) {
			this.c.texture = RES.getRes("result-3_png");
			this.addChild(this.c);
			this.c.width = 244;
			this.c.height = 76;
			this.c.y = 503;
			this.c.x = 198;
			this.c.touchEnabled = true;
			this.c.addEventListener(egret.TouchEvent.TOUCH_BEGIN, function () {
				this.removeChild(this.c);
				this.removeChild(this.group2);
				this.game7.play(-1);
			}, this);
		}
	}
	qiehuan() {
		this.removeChild(this.group3);
		this.game8.play(-1);
	}
	Label1() {
		this.addChild(this.group5);
		this.game9.play(-1);
	}
	Label3() {
		this.game10.play(-1);
		this.removeChild(this.c);
	}
	Group5() {
		this.removeChild(this.group5);
	}
	time() {
		this.prama.pro -= 1;
		console.log(this.prama.pro);


		if ((this.prama.pro) == 0) {
			this.timer4.stop();
			this.timer5.stop();
			this.timer8.stop();
			this.game3.play(-1);
			this.timer11.stop();
			this.number.removeEventListener(egret.Event.ENTER_FRAME, this.text, this);
		}
	}
	//碰撞判断
	pengzhuan() {
		if (this.isCollsion()) {
			if (this.kaiguan == true) {
				this.removeChild(this.c2);
				this.prama.pro += 4;
				this.kaiguan = false;
			}
		}else{
			this.kaiguan = true;
		}

		if (this.isCollsionWithRect()) {
			this.timer4.stop();
			this.timer5.stop();
			this.timer8.stop();
			this.game3.play(-1);
			this.timer11.stop();
			this.number.removeEventListener(egret.Event.ENTER_FRAME, this.text, this);
		}
	}
	//云的碰撞
	public isCollsionWithRect() {
		if (this.image.x >= this.c1.x && this.image.x >= this.c1.x + this.c1.width) {
			return false;
		} else if (this.image.x <= this.c1.x && this.image.x + this.image.width <= this.c1.x) {
			return false;
		} else if (this.image.y >= this.c1.y && this.image.y >= this.c1.y + this.c1.height) {
			return false;
		} else if (this.image.y <= this.c1.y && this.image.y + this.image.height <= this.c1.y) {
			return false;
		}
		return true;
	}
	//瓶子的碰撞
	public isCollsion() {
		if (this.image.x >= this.c2.x && this.image.x >= this.c2.x + this.c2.width) {
			return false;
		} else if (this.image.x <= this.c2.x && this.image.x + this.image.width <= this.c2.x) {
			return false;
		} else if (this.image.y >= this.c2.y && this.image.y >= this.c2.y + this.c2.height) {
			return false;
		} else if (this.image.y <= this.c2.y && this.image.y + this.image.height <= this.c2.y) {
			return false;
		}
		return true;
	}
}